package com.spyu.exception;

import com.spyu.model.ResultModel;
import com.spyu.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常处理器
 *
 * @author spyu
 * @date 2023-03-08
 */
@Slf4j
@RestControllerAdvice
public class CommonExceptionHandler {

    /**
     * 异常处理
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler
    public ResultModel<?> handleException(Exception e, HttpServletRequest request) {
        ResultModel<String> resultModel = new ResultModel<>();
        resultModel.setCode(500);
        resultModel.setStatus(false);
        resultModel.setRequestPath(request.getRequestURI());
        resultModel.setResponseTime(DateUtils.getCurrentDateTime());
        resultModel.setMessage(e.getMessage());
        resultModel.setT(e.getMessage());
        log.error("服务器处理失败，{}", resultModel);
        return resultModel;
    }
}
